package apiexample.transform.statistics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import apiexample.transform.CentralDataPool;

import com.apiexample.server.dao.BaseDao;
import com.apiexample.server.dao.ClassDao;
import com.apiexample.server.dao.ProjectDao;
import com.apiexample.server.entity.ClassEntity;
import com.apiexample.server.entity.ProjectEntity;

/**
 * Build the correlation between APIs and projects
 * 
 * @author lijiejacy
 * 
 */
public class CorrelateAPIProject {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connection server_conn = BaseDao.getTestConn();
		Connection example_conn = dao.BaseDao.getTestConn();
		CentralDataPool.persist(example_conn,server_conn);

		Connection conn = BaseDao.getTestConn();

		// HashMap<Integer,ClassEntity> classIdEntityMap = new HashMap<Integer,ClassEntity> ();
		// List<ClassEntity> classes = ClassDao.getAllClassList(conn);
		// for (ClassEntity classEntity : classes) {
		// classIdEntityMap.put(new Integer(classEntity.getId()), classEntity);
		// }

		File projectRoot = new File("C:/Users/lijiejacy/Desktop/实验对象/ProjectClassNames/projects/");
		File[] projectFiles = projectRoot.listFiles();
		HashSet<String> projects = new HashSet<String>();
		int projectCount = 0;
		for (File file : projectFiles) {
			String fileName = file.getName();
			String projectNameStr = fileName.substring(0, fileName.length() - 4);
			String[] ss = projectNameStr.split("_");
			String projectName = ss[0];
			String version = ss[1];
			if (projects.add(projectName)) {
				projectCount++;
			}

			ProjectEntity pe = new ProjectEntity(projectCount, projectName, "", version, "");
			ProjectDao.insertProject(conn, pe);

			HashSet<String> apiNames = new HashSet<String>();
			try {
				BufferedReader br = new BufferedReader(new FileReader(file));
				String line = null;
				while ((line = br.readLine()) != null) {
					if (line.trim().length() > 0) {
						apiNames.add(line);
					}
				}
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			for (String apiName : apiNames) {
				Integer apiID_server = CentralDataPool.classNameMapID.get(apiName);
				if (apiID_server != null) {
					// ClassEntity entity = classIdEntityMap.get(apiID_server);
					ClassEntity classEntity = new ClassEntity(apiID_server.intValue(), null, -1, -1, projectCount);
					if (classEntity != null) {
						classEntity.setProject_id(projectCount);
						ClassDao.updateClass(conn, classEntity);
						System.out.println("Insert " + apiName + " " + projectName);
					}
				}
			}
			System.out.println(projectName);
		}

		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
